package com.example.demo.common.handle;

import com.example.demo.common.exception.BusinessException;
import com.example.demo.common.exception.ExceptionResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * @Name GlobalException
 * @Description 全局异常处理器
 * @Author wen
 * @Date 2020-03-28
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<ExceptionResult> handleIllegalArgumentException(BusinessException ex, HttpServletRequest request){
        log.error("【业务处理异常】：错误码={}，错误信息={}" , ex.getCode(), ex.getMsg() );
        return ResponseEntity.ok().body(ExceptionResult.build(ex));
    }
}
